package swing;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;

import engine.Control;
import engine.EventObserver;

public class Observers {

	public static EventObserver<KeyEvent> addKeyObserver(JComponent sourceCompoent, Control gameControl) {
		final EventObserver<KeyEvent> keyObserver = new EventObserver<KeyEvent>();
		sourceCompoent.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {
				keyObserver.addEvent(e);
			}

			@Override
			public void keyReleased(KeyEvent e) {
				keyObserver.addEvent(e);
			}

			@Override
			public void keyPressed(KeyEvent e) {
				keyObserver.addEvent(e);
			}
		});
		gameControl.addObserver(keyObserver);
		return keyObserver;
	}

	public static EventObserver<MouseEvent> addMouseObserver(JComponent sourceCompoent, Control gameControl) {
		final EventObserver<MouseEvent> mouseObserver = new EventObserver<MouseEvent>();
		sourceCompoent.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				mouseObserver.addEvent(e);
			}

			@Override
			public void mousePressed(MouseEvent e) {
				mouseObserver.addEvent(e);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				mouseObserver.addEvent(e);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				mouseObserver.addEvent(e);
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				mouseObserver.addEvent(e);
			}
		});
		sourceCompoent.addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseMoved(MouseEvent e) {
				mouseObserver.addEvent(e);
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				mouseObserver.addEvent(e);
			}
		});
		sourceCompoent.addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseMoved(MouseEvent e) {
				mouseObserver.addEvent(e);
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				mouseObserver.addEvent(e);
			}
		});
		gameControl.addObserver(mouseObserver);
		return mouseObserver;
	}
}
